package com.morningstar.system.init;

import com.morningstar.system.dao.entity.SysConfig;
import com.morningstar.system.dao.mapper.SysConfigMapper;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 项目启动初始化
 * @author yls
 */
@Slf4j
@Order(value = 1)
@Component
public class SystemServiceRunner implements ApplicationRunner {

    @Resource
    RedisTemplate<String, String> redisTemplate;

    @Resource
    private SysConfigMapper sysConfigMapper;

    @Override
    public void run(ApplicationArguments args) throws Exception {
//        log.info("user-passwd {}", BCrypt.hashpw("123456"));
        log.info("初始化配置信息开始");
        List<SysConfig> configList = sysConfigMapper.selectList(null);
        configList.forEach(sysConfig -> {
            redisTemplate.opsForValue().set(sysConfig.getConfigKey(), sysConfig.getConfigValue());
            log.info("初始化配置信息 {} {}", sysConfig.getConfigKey(), redisTemplate.opsForValue().get(sysConfig.getConfigKey()));
        });
    }
}
